function refresh(page) {
    $.ajax({
        // 请求地址
        url: "/user/list",
        data: {page},
        // success : 当成功从请求地址那获取到数据之后执行的方法
        success(response) {
            // 获取后端数据库中的总数
            let count = response.count;
            // 获取后端数据库中的列表数据
            let users = response.users;
            // 修改页面上总条数的数字
            stotal.innerHTML = count;

            let html = '';
            for (let i = 0; i < users.length; i++) {
                let u = users[i];
                let s = u.disabled ? "<b style='color: black' id='jy'>禁用</b>" : "<b style='color: green' id='qy'>启用</b>";
                let c = u.disabled == 0 ? "<button style='color: purple' onclick='useredit(this)' test='" + u.id + "'>编辑</button><button style='color: black' onclick='userdisable(this)' test='" + u.id + "'>禁用</button>" : "<button style='color: purple' onclick='useredit(this)' test='" + u.id + "'>编辑</button><button style='color: black' onclick='use(this)' test='" + u.id + "'>启用</button>";
                html += `
                <tr>
                    <td>${u.username}</td>
                    <td>${s}</td>
                    <td>${c}</td>
                </tr>
            `;
            }
            userlistbody.innerHTML = html;
        }
    });
}

window.addEventListener("load", function () {
    // 为 window 添加事件监听（load），当页面加载完成之后调用 refresh
    refresh(1);
});

function useredit(id) {
    let nowId = $(id).attr("test")
    if ( nowId !== undefined && nowId != null){
        id = Number.isInteger(parseInt(nowId)) ? nowId : 0
        $.ajax({
            url: '/user/nowIdInfo',
            data: {id},
            success(response) {
            }
        })
    }
    location.href="/?target=resume"
}

function use (id){
    let nowId = $(id).attr("test")
    id = Number.isInteger(parseInt(nowId)) ? nowId : 0
    $.ajax({
        url:"/user/use",
        data:{id},
        success(res){

        }
    })
    location.href="/?target=user-list"

}

function userdisable(id){
    let nowId = $(id).attr("test")
    id = Number.isInteger(parseInt(nowId)) ? nowId : 0
    $.ajax({
        url:"/user/disable",
        data:{id},
        success(res){

        }
    })
    location.href="/?target=user-list"
}